package grape.crm.rest.customer.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.base.rest.client.user.UserClient;
import grape.base.service.user.po.User;
import grape.common.exception.runtime.RBaseException;
import grape.crm.rest.customerassociatedcontactperson.mapper.CustomerAssociatedContactPersonWebMapper;
import grape.crm.rest.customerbelong.mapper.CustomerBelongWebMapper;
import grape.crm.rest.customercontact.form.CustomerContactCreateForm;
import grape.crm.rest.customercontact.mapper.CustomerContactWebMapper;
import grape.crm.rest.customercredential.mapper.CustomerCredentialWebMapper;
import grape.crm.service.customer.param.AddCustomerParam;
import grape.crm.service.customerbelong.api.ICustomerBelongService;
import grape.crm.service.customerbelong.po.CustomerBelong;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.crm.rest.customer.form.CustomerCreateForm;
import grape.crm.rest.customer.form.CustomerUpdateForm;
import grape.crm.rest.customer.form.CustomerListPageForm;
import grape.crm.rest.customer.vo.CustomerVo;
import grape.crm.rest.customer.mapper.CustomerWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.crm.service.customer.po.Customer;
import grape.crm.service.customer.api.ICustomerService;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
/**
 * <p>
 * 客户表 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-14
 */
@RestController
@RequestMapping("/crm/customer")
@Api(tags = "客户相关接口")
public class CustomerController extends BaseController<CustomerVo, Customer> {

    @Autowired
    private CustomerWebMapper currentWebMapper;
    @Autowired
    private ICustomerService currentService;
    @Autowired
    private ICustomerBelongService iCustomerBelongService;
    @Autowired
    private CustomerContactWebMapper customerContactWebMapper;
    @Autowired
    private CustomerCredentialWebMapper customerCredentialWebMapper;
    @Autowired
    private CustomerBelongWebMapper customerBelongWebMapper;
    @Autowired
    private CustomerAssociatedContactPersonWebMapper customerAssociatedContactPersonWebMapper;


     @ApiOperation("添加客户表")
     @PreAuthorize("hasAuthority('customer:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public CustomerVo create(@RequestBody @Valid CustomerCreateForm cf,@ApiIgnore String loginUserId) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }

         AddCustomerParam addCustomerParam = currentWebMapper.formToParam(cf);
         addCustomerParam.setBelongUserId(loginUserId);
         addCustomerParam.setCustomerContacts(customerContactWebMapper.attrCreateFormsToParams(cf.getCustomerContactCreateForms()));
         addCustomerParam.setCustomerCredentials(customerCredentialWebMapper.attrCreateFormsToParams(cf.getCustomerCredentialCreateForms()));
         addCustomerParam.setCustomerAssociatedContactPersons(customerAssociatedContactPersonWebMapper.attrCreateFormsToParams(cf.getCustomerAssociatedContactPersonCreateForms()));
         Customer customer = currentService.addCustomer(addCustomerParam);
         CustomerVo customerVo = super.returnCreate(customer);

         return customerVo;
     }

     @ApiOperation("根据ID查询客户表")
     @PreAuthorize("hasAuthority('customer:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public CustomerVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除客户表")
     @PreAuthorize("hasAuthority('customer:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新客户表")
     @PreAuthorize("hasAuthority('customer:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public CustomerVo update(@PathVariable String id,@RequestBody @Valid CustomerUpdateForm uf) {
         Customer po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("分页查询客户表")
    @PreAuthorize("hasAuthority('customer:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<CustomerVo> listPage(CustomerListPageForm listPageForm) {
         Customer po = currentWebMapper.formToPo(listPageForm);
         return super.listPage(po,listPageForm);
     }

    @Override
    public CustomerVo transVo(CustomerVo customerVo) {
        CustomerBelong byCustomerId = iCustomerBelongService.getByCustomerId(customerVo.getId());
        customerVo.setCustomerBelong(customerBelongWebMapper.poToVo(byCustomerId));
        return super.transVo(customerVo);
    }
}
